package clientView;

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import objectView.EmployeeInfo;
import utility.StringUtil;
import utility.TextConverter;
import clientMain.Main;
import clientUtilGUI.UtilityGUI;
import database.Paging;

public class Employee_Search extends JScrollPane implements ActionListener{

	private JPanel panel;
	private JLabel lblTitle;
	private JButton btnFind;
	private JButton btnCancel;
	private JLabel lblChooser;
	private JRadioButton rdbtnId;
	private JRadioButton rdbtnName;
	private JRadioButton rdbtnPos;
	private ButtonGroup groupChooser;
	private JTextField txtSearch;
	private JLabel lblResult;
	private GridBagConstraints gbc_lblResult;
	private static JTable tbl;
	private int pageCur = 1;
	private JPanel panelPage;
	private JButton btnBack;
	private JComboBox cbbPage;
	private JButton btnNext;
	private Vector<EmployeeInfo> list;
	private String column;
	private String search;
	private GridBagConstraints gbc_tbl;
	
	public Employee_Search(){
		setViewportView(initComponent());		
		setBorder(new EmptyBorder(0, 0, 0, 0));
	}
		
	private JPanel initComponent() {
		panel = new JPanel();
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[]{0, 0, 0, 0};
		gbl_panel.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
		gbl_panel.columnWeights = new double[]{0.0, 0.0, 0.0, 1.0};
		gbl_panel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0};
		panel.setLayout(gbl_panel);
		
		lblTitle = new JLabel("Tìm Kiếm Nhân Viên");
		lblTitle.setFont(UtilityGUI.fontHeader1);
		lblTitle.setForeground(UtilityGUI.colorHeader1);
		GridBagConstraints gbc_lblTitle = new GridBagConstraints();
		gbc_lblTitle.gridwidth = 4;
		gbc_lblTitle.insets = new Insets(10, 20, 5, 20);
		gbc_lblTitle.gridx = 0;
		gbc_lblTitle.gridy = 0;
		panel.add(lblTitle, gbc_lblTitle);
		
		lblChooser = new JLabel("Theo:");
		lblChooser.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblChooser = new GridBagConstraints();
		gbc_lblChooser.anchor = GridBagConstraints.WEST;
		gbc_lblChooser.insets = new Insets(5, 20, 5, 5);
		gbc_lblChooser.gridx = 0;
		gbc_lblChooser.gridy = 1;
		panel.add(lblChooser, gbc_lblChooser);
		
		rdbtnId = new JRadioButton("Mã NV");
		rdbtnId.setFont(UtilityGUI.fontLblForm);
		rdbtnId.setSelected(true);
		GridBagConstraints gbc_rdbtnId = new GridBagConstraints();
		gbc_rdbtnId.anchor = GridBagConstraints.WEST;
		gbc_rdbtnId.insets = new Insets(5, 0, 5, 5);
		gbc_rdbtnId.gridx = 1;
		gbc_rdbtnId.gridy = 1;
		panel.add(rdbtnId, gbc_rdbtnId);
		
		rdbtnName = new JRadioButton("Tên");
		rdbtnName.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_rdbtnName = new GridBagConstraints();
		gbc_rdbtnName.anchor = GridBagConstraints.WEST;
		gbc_rdbtnName.insets = new Insets(5, 0, 5, 5);
		gbc_rdbtnName.gridx = 2;
		gbc_rdbtnName.gridy = 1;
		panel.add(rdbtnName, gbc_rdbtnName);
		
		rdbtnPos = new JRadioButton("Chức Vụ");
		rdbtnPos.setFont(UtilityGUI.fontBtnForm);
		GridBagConstraints gbc_rdbtnPos = new GridBagConstraints();
		gbc_rdbtnPos.anchor = GridBagConstraints.WEST;
		gbc_rdbtnPos.insets = new Insets(5, 0, 5, 20);
		gbc_rdbtnPos.gridx = 3;
		gbc_rdbtnPos.gridy = 1;
		panel.add(rdbtnPos, gbc_rdbtnPos);
		
		groupChooser = new ButtonGroup();
		groupChooser.add(rdbtnId);
		groupChooser.add(rdbtnName);
		groupChooser.add(rdbtnPos);
		
		txtSearch = new JTextField();
		txtSearch.setColumns(55);
		txtSearch.addFocusListener(new FocusAdapter() {
			public void focusLost(FocusEvent e){
				txtSearch.setText(StringUtil.removeUnwantedSpace(txtSearch.getText()));
			};
		});
		GridBagConstraints gbc_txtSearch = new GridBagConstraints();
		gbc_txtSearch.anchor = GridBagConstraints.WEST;
		gbc_txtSearch.gridwidth = 4;
		gbc_txtSearch.insets = new Insets(0, 20, 5, 20);
		gbc_txtSearch.gridx = 0;
		gbc_txtSearch.gridy = 2;
		panel.add(txtSearch, gbc_txtSearch);		
		
		btnFind = new JButton(UtilityGUI.btnFind);
		GridBagConstraints gbc_btnFind = new GridBagConstraints();
		gbc_btnFind.anchor = GridBagConstraints.WEST;
		gbc_btnFind.insets = new Insets(0, 20, 5, 5);
		gbc_btnFind.gridx = 0;
		gbc_btnFind.gridy = 3;
		panel.add(btnFind, gbc_btnFind);
		btnFind.setFont(UtilityGUI.fontBtnForm);
		btnFind.addActionListener(this);
		
		btnCancel = new JButton(UtilityGUI.btnCancel);
		GridBagConstraints gbc_btnCancel = new GridBagConstraints();
		gbc_btnCancel.anchor = GridBagConstraints.WEST;
		gbc_btnCancel.insets = new Insets(0, 0, 5, 5);
		gbc_btnCancel.gridx = 1;
		gbc_btnCancel.gridy = 3;
		panel.add(btnCancel, gbc_btnCancel);
		btnCancel.setFont(UtilityGUI.fontBtnForm);
		btnCancel.addActionListener(this);
		
		lblResult = new JLabel("");
		lblResult.setFont(UtilityGUI.fontLblForm);
		lblResult.setForeground(UtilityGUI.colorNote);
		gbc_lblResult = new GridBagConstraints();
		gbc_lblResult.anchor = GridBagConstraints.WEST;
		gbc_lblResult.gridwidth = 4;
		gbc_lblResult.insets = new Insets(3, 20, 5, 20);
		gbc_lblResult.gridx = 0;
		gbc_lblResult.gridy = 4;
		panel.add(lblResult, gbc_lblResult);
		
		return panel;
	}
	
	private void getPanelPage(){
		panelPage = new JPanel();
		GridBagConstraints gbc_panelPage = new GridBagConstraints();
		gbc_panelPage.gridwidth = 4;
		gbc_panelPage.insets = new Insets(3, 20, 10, 20);
		gbc_panelPage.fill = GridBagConstraints.CENTER;
		gbc_panelPage.gridx = 0;
		gbc_panelPage.gridy = 6;
		panel.add(panelPage, gbc_panelPage);
		panelPage.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		btnBack = new JButton(UtilityGUI.iconBack);
		btnBack.addActionListener(this);
		panelPage.add(btnBack);
		
		cbbPage = UtilityGUI.cbbPaging(column, search);
		cbbPage.addActionListener(this);
		panelPage.add(cbbPage);
		
		btnNext = new JButton(UtilityGUI.iconNext);
		btnNext.addActionListener(this);
		panelPage.add(btnNext);
	}
	
	private JTable tblSearch(Vector<EmployeeInfo> list) {
		DefaultTableModel model = new DefaultTableModel();
		String[] columnNames = {"", "", "", "", ""};
		Object[][] rowdata = {};
		model.setDataVector(rowdata, columnNames);
		for(int i = 0; i<list.size(); i++){
			EmployeeInfo emInfo = list.get(i);			
			Object[] em = {emInfo.getEmId(), emInfo.getEmFullName(),
					emInfo.getEmMobile(), emInfo.getDepName(), emInfo.getPosName()};
			model.insertRow(i, em);
		}

		JTable tblS = new JTable(model){
			public boolean isCellEditable(int rowIndex, int colIndex) {
				return false; //Disallow the editing of any cell
			}
		};
		tblS.getColumnModel().getColumn(0).setPreferredWidth(70);
		tblS.getColumnModel().getColumn(1).setPreferredWidth(175);
		tblS.getColumnModel().getColumn(2).setPreferredWidth(90);
		tblS.getColumnModel().getColumn(3).setPreferredWidth(175);
		tblS.getColumnModel().getColumn(4).setPreferredWidth(140);
		tblS.setFont(UtilityGUI.fontTbl);
		tblS.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		tblS.setToolTipText(UtilityGUI.moreInfo);
		tblS.getTableHeader().setReorderingAllowed(false);
	    tblS.getTableHeader().setResizingAllowed(false);
	    tblS.setCursor(UtilityGUI.cursorBtn);
	    tblS.setRowHeight(21);
	    tblS.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
	    tblS.setBackground(getBackground());
	    tblS.setShowHorizontalLines(false);
	    tblS.setShowVerticalLines(false);
	    return tblS;
	}
	
	public static class Mouse_tblEmSearch extends MouseAdapter{
		public void mouseClicked(MouseEvent e) {
			int row = tbl.getSelectedRow();
			String emId  = (String) tbl.getModel().getValueAt(row, 0);
			if(e.isControlDown() && e.getButton() == e.BUTTON1){
				int index = Main.jTabbedPane.positionTab(UtilityGUI.tabEmInfo);
				if(index == -1){
					Main.jTabbedPane.addTab(new Employee_View(emId), UtilityGUI.tabEmInfo);
					Main.jTabbedPane.setSelectedIndex(Main.jTabbedPane.getTabCount()-1);
				}else{
					Main.jTabbedPane.setSelectedIndex(index);
					return;
				}
			}
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnFind){
			lblResult.setText("");
			if(tbl != null)	tbl.setVisible(false);
			if(panelPage != null) panelPage.setVisible(false);
			if(rdbtnId.isSelected()){
				column = "Em_Id";
				if(!StringUtil.isValidEmId(txtSearch.getText())){
					lblResult.setText(UtilityGUI.errorValidEmId);
					txtSearch.requestFocusInWindow();
					return;
				}
			}
			if(rdbtnName.isSelected()){
				column = "Em_FullName";
				if(!StringUtil.isValidName(txtSearch.getText(), 100, 2)){
					String error = UtilityGUI.errorValidName(rdbtnName.getText(),100,2);
					lblResult.setText(error);
					txtSearch.requestFocusInWindow();
					return;
				}
			}
			if(rdbtnPos.isSelected()){
				column = "Pos_Name";
				if(!StringUtil.isValidName(txtSearch.getText(), 100, 2)){
					String error = UtilityGUI.errorValidName(rdbtnPos.getText(),100,2);
					lblResult.setText(error);
					txtSearch.requestFocusInWindow();
					return;
				}
			}
			lblResult.setText("");
			search = txtSearch.getText();
			search = StringUtil.removeAllSpace(TextConverter.toEnglish(search)).toUpperCase();
			list = Main.viewEm.searchEm(pageCur , column, search);
			
			int total = Paging.getTotalRecord(column, search);
			lblResult.setText("Có "+total+" kết quả tìm được.");
			if(total >0){
				tbl = tblSearch(list);
				tbl.addMouseListener(new Mouse_tblEmSearch());
			    gbc_tbl = new GridBagConstraints();
				gbc_tbl.anchor = GridBagConstraints.NORTHWEST;
				gbc_tbl.insets = new Insets(3, 20, 5, 20);
				gbc_tbl.gridx = 0;
				gbc_tbl.gridy = 5;
				gbc_tbl.gridwidth = 4;
				panel.add(tbl, gbc_tbl);
				getPanelPage();
		    }
			
		}else if(e.getSource() == btnCancel){
			Main.jTabbedPane.remove(this);
		}else if(e.getSource() == btnBack){
			if(pageCur == 1){
				return;
			}
			tbl.setVisible(false);
			pageCur = pageCur - 1;
			cbbPage.setSelectedItem(pageCur);
			list = Main.viewEm.searchEm(pageCur , column, search);
			tbl = tblSearch(list);
			tbl.addMouseListener(new Mouse_tblEmSearch());
			panel.add(tbl, gbc_tbl);
			
		}else if(e.getSource() == btnNext){
			if(pageCur == cbbPage.getItemCount()){
				return;
			}
			tbl.setVisible(false);
			pageCur = pageCur + 1;
			cbbPage.setSelectedItem(pageCur);
			list = Main.viewEm.searchEm(pageCur , column, search);
			tbl = tblSearch(list);
			tbl.addMouseListener(new Mouse_tblEmSearch());
			panel.add(tbl, gbc_tbl);

		}else if(e.getSource() == cbbPage){
			int pageGo = (Integer) cbbPage.getSelectedItem();
			if(pageCur == pageGo){
				return;
			}else{
				pageCur = pageGo;
				tbl.setVisible(false);
				list = Main.viewEm.searchEm(pageCur , column, search);
				tbl = tblSearch(list);
				tbl.addMouseListener(new Mouse_tblEmSearch());
				panel.add(tbl, gbc_tbl);
				
			}
		}
	}

}
